package com.rongc.client.freight.business.carrier.view.activity;

import android.os.Bundle;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.rongc.client.core.gson.GsonHelper;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;
import com.rongc.client.freight.business.carrier.request.api.ConveyanceCancelApi;
import com.rongc.client.freight.business.carrier.view.fragment.ConveyanceSupplyFragment;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.model.SupplyMyList;
import com.rongc.client.freight.business.supply.request.api.SupplyFindApi;
import com.rongc.client.freight.business.supply.view.fragment.SupplyFragment;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by Bruce on 2017/7/7.
 */
public class ConveyanceWaybillActivity extends BaseActivity implements View.OnClickListener{
    ConveyanceSupplyFragment fragment;
    String conveyanceid;
    ConveyanceBean conveyanceBean;
    List<SupplyBean> supplyBeanList;

    SupplyFragment supplyFragment;

    NormalDialog dialog;
    int page = 1;


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_status:
                dialog.show();
                break;
        }
    }

    Response.Listener<JSONObject> respWorkersListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                try {
                    conveyanceBean = GsonHelper.getDeserializer().fromJson(response.getString("trucksInfo"), ConveyanceBean.class);
                    SupplyMyList atemp = GsonHelper.getDeserializer().fromJson(response.getString("goodsInfos"), SupplyMyList.class);
                    supplyBeanList.clear();
                    if(atemp != null && atemp.getList() != null)
                        supplyBeanList.addAll(atemp.getList());

                    fragment.setDatas(supplyBeanList,page);

                    supplyFragment.setData(conveyanceBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceWaybillActivity.this);
            }
        }
    };

    Response.Listener<NullResult> respWaybillListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                dialog.cancel();
                finish();
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceWaybillActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceWaybillActivity.this);
        }
    };

    @Override
    public void callbackFun1(Bundle arg) {
        page = arg.getInt("page",page);
        doFind(page);
    }

    void doFind(int page){
        this.page = page;
        SupplyFindApi p = new SupplyFindApi(new SupplyFindApi.SupplyFindParams(
                conveyanceid,page+""),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void initData() {
        doFind(1);
    }

    @Override
    public void initParams() {
        conveyanceid = getIntent().getStringExtra("data");
    }

    @Override
    public void initView() {

        initToolbar(R.string.conveyance_waybill_title);
        supplyBeanList = new ArrayList<>();
        fragment = ConveyanceSupplyFragment.getInstance(conveyanceid);
        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        supplyFragment = (SupplyFragment) getSupportFragmentManager().findFragmentByTag("supply");
        supplyFragment.setListener(this);

        makeDialog();
    }

    void makeDialog(){
        dialog = new NormalDialog(this);
        dialog.content(getResources().getString(R.string.conveyance_supply_dialog_content))
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        //  取消绑定
                        ConveyanceCancelApi p = new ConveyanceCancelApi(new ConveyanceCancelApi.ConveyanceCancelParams(
                                conveyanceid),
                                respWaybillListener, errorListener);
                        RequestManager.getInstance().call(p);

                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_conveyance_waybill;
    }
}
